package
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import it.gotoandplay.smartfoxserver.SFSEvent;
	
	import mx.core.BitmapAsset;
	
	public class EnginesShop extends SubShop
	{
		private var shopScreen:ShopScreen;
		
		private var coatlImage:BitmapAsset;
		private var background:Shape;
		private var canvas:Sprite;
				
		private var textFields:Sprite;
		
		private var textFieldInitPosX:Number;
		private var textFieldInitPosY:Number;
		
		private var confirmPopUp:ConfirmPopUp;
		
		private var engines:Array;
		private var engineToBuy:Engine;
		private var selectedEngine:Engine;
		private var selectedTextFieldContainer:Object;
		
		private var mouseDown:Boolean;
		private var previousMouseY:int;
				
		public function EnginesShop(game:DRace, shopScreen:ShopScreen)
		{
			super();
		
			this.shopScreen = shopScreen;
			addEventListener(Event.ADDED_TO_STAGE,onShopOpened);
			
			subShopCanvas.addEventListener(MouseEvent.CLICK,onMouseClick);
			subShopCanvas.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			subShopCanvas.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			subShopCanvas.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			canvas = new Sprite();
			canvas.scrollRect = new Rectangle(0,0,subShopCanvas.width,subShopCanvas.height);
			 
			engines = new Array();
			textFields = new Sprite();
			textFields.mouseChildren = true;
			
						
			//close events	
			header.btnClose.addEventListener(MouseEvent.CLICK,onShopClosed);
			header.btnClose.addEventListener(MouseEvent.ROLL_OVER,onCloseRollOver);
			header.btnClose.addEventListener(MouseEvent.ROLL_OUT,onCloseRollOut);
			
		
			//creo el rect de scroll para scrollear luego
			
			
			//agrego una imagen x para probar el scrolling y la mascara
			coatlImage = new BitmapAsset(new Coatl(0,0));	
			canvas.addChild(coatlImage);
			
			//creo el confirmPopUp
			confirmPopUp = new ConfirmPopUp();
			selectedEngine = null;
			engineToBuy = null;
			
			this.subShopCanvas.addChild(canvas);
			
		}
		public function setLanguage(xmlFile:XML):void
		{
			header.label.text = xmlFile.ShopScreen.EnginesShop.attribute("header");
			
			confirmPopUp.label.text = xmlFile.ShopScreen.ConfirmPopUp.attribute("label");
			confirmPopUp.btnYes.label.text = xmlFile.ShopScreen.ConfirmPopUp.attribute("btnYes");
			confirmPopUp.btnNo.label.text =  xmlFile.ShopScreen.ConfirmPopUp.attribute("btnNo");
		}
		public function update():void
		{
			/*		
			var rect:Rectangle = canvas.scrollRect;
			rect.y++;
			canvas.scrollRect = rect;
			*/
		}
		private function onShopOpened(ev:Event):void
		{
			
			DRace.sfc.addEventListener(SFSEvent.onExtensionResponse,this.onExtensionResponse);
			DRace.sfc.sendXtMessage("simpleExt","getEngines",DRace.player,"xml");
			
		}
		private function onExtensionResponse(ev:SFSEvent):void
		{
			clearTextFields();
			for each (var i:Object in ev.params.dataObj.engines)
			{
				var newEngine:Engine = new Engine();
				newEngine.id = i.id;
				newEngine.acel = i.acel;
				newEngine.maxSpeed = i.maxSpeed;
				newEngine.name = i.name;
				newEngine.price = i.price;
				engines.push(newEngine);			
				addTextField(i.id.toString(),i.name.toString(),i.acel.toString(),i.maxSpeed.toString(),i.price.toString());
			}
			DRace.sfc.removeEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
		}
		private function onShopClosed(ev:MouseEvent):void
		{
			DRace.sfc.removeEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
			if (parent)
				parent.removeChild(this);
		}
		private function onCloseRollOver(ev:MouseEvent):void
		{
			header.btnClose.textColor = 0xFFFFFF;
		}
		private function onCloseRollOut(ev:MouseEvent):void
		{
			header.btnClose.textColor = 0xFF0000;
		}
		
		private function clearTextFields():void
		{
			if (canvas.contains(textFields))
				canvas.removeChild(textFields);
			textFields = new Sprite();
			engines = new Array();
			canvas.addChild(textFields);
		}
		private function createTextField(text:String,posX:int,posY:int,width:Number,height:Number):TextField
		{
			var textField:TextField = new TextField();
			textField.selectable = false;
			textField.background = true;
			textField.backgroundColor = 0x777777;
			textField.border = true;
			textField.borderColor = 0xFF0000;
			textField.width = width;
			textField.height = height;
			textField.x = posX;
			textField.y = posY;
			textField.text = text;	
			
			return textField;							
		}
		private function addTextField(id:int,model:String,acel:String,
		maxSpeed:String,price:String):void
		{
			const textFieldHeight:int = 50;
			var textFieldSpriteContainer:Sprite = new Sprite();
			textFieldSpriteContainer.mouseChildren = false;
			textFieldSpriteContainer.x = 2;
			textFieldSpriteContainer.y = textFields.numChildren*textFieldHeight;
			
			var modelText:TextField = createTextField(model,0,0,header.width/4,textFieldHeight);
			var acelText:TextField = createTextField(acel,modelText.width + modelText.x, 0 , header.width/4, modelText.height);
			var maxSpeedText:TextField = createTextField(maxSpeed,acelText.x + acelText.width, 0 , header.width/4, modelText.height);
			var priceText:TextField	= createTextField(price,maxSpeedText.x + maxSpeedText.width, 0, header.width/4 , modelText.height);
						
			textFieldSpriteContainer.addChild(modelText);
			textFieldSpriteContainer.addChild(acelText);
			textFieldSpriteContainer.addChild(maxSpeedText);
			textFieldSpriteContainer.addChild(priceText);
						
			textFieldSpriteContainer.addEventListener(MouseEvent.ROLL_OVER,onMouseOverTextField);
			textFieldSpriteContainer.addEventListener(MouseEvent.ROLL_OUT,onMouseOutTextField);
			textFieldSpriteContainer.addEventListener(MouseEvent.CLICK,onMouseClickTextField);
			
			this.textFields.addChild(textFieldSpriteContainer);
		}
		private function confirmButtonYes(ev:MouseEvent):void
		{
			//si tiene plata mandar el mensaje a la base y agregarlo a la variable.
			if (engineToBuy)
			{
				 if (engineToBuy.price <= DRace.player.money)
				 {
				 	DRace.player.money-=engineToBuy.price;
				 	DRace.player.car.engine = engineToBuy;
				 
				 	var obj:Object = {};
				 	obj.userId = DRace.player.id;
				 	obj.engineId = engineToBuy.id;
				 	obj.moneyLeft = DRace.player.money;
				 				 	
				 	DRace.sfc.sendXtMessage("simpleExt","buyEngine",obj);
							 	
				 		shopScreen.changeStatusPlayer();
					shopScreen.updateMoneyInGui("Engine");
				 }
				removeConfirmPopUp();
			}
		}
		private function confirmButtonNo(ev:MouseEvent):void
		{
			removeConfirmPopUp();
			
		}
		private function removeConfirmPopUp():void
		{
			this.canvas.mouseChildren = true;
			shopScreen.removeChild(confirmPopUp);
			shopScreen.changeStatusEngine(null);
			engineToBuy = null;
		}
		private function onMouseClickTextField(ev:MouseEvent):void
		{
			if (selectedEngine)
			{
				engineToBuy = selectedEngine;
				confirmPopUp.btnYes.addEventListener(MouseEvent.CLICK,confirmButtonYes);
				confirmPopUp.btnNo.addEventListener(MouseEvent.CLICK,confirmButtonNo);
				
				confirmPopUp.x = shopScreen.shopCanvas.x;
				confirmPopUp.y = this.mouseY;
				this.canvas.mouseChildren = false;
				
				shopScreen.addChild(confirmPopUp);
				//this.addChild(confirmPopUp);
			}
				
		}
	
		private function onMouseOverTextField(ev:MouseEvent):void
		{
			selectedTextFieldContainer = ev.target;
			selectedEngine = engines[textFields.getChildIndex(selectedTextFieldContainer as DisplayObjectContainer)]
			var price:Number =  selectedEngine.price;
		
			if (price <= DRace.player.money)
			{
				for (var i:int = 0; i<ev.target.numChildren;++i)
				{
					selectedTextFieldContainer.getChildAt(i).backgroundColor = 0x123456;
					selectedTextFieldContainer.getChildAt(i).textColor = 0xFFFFFF;
				}
				shopScreen.changeStatusEngine(selectedEngine);
			}	
			else
			{
				selectedEngine = null;
				selectedTextFieldContainer = null;
			}
		}
		private function onMouseOutTextField(ev:MouseEvent):void
		{
			for (var i:int = 0;i<ev.target.numChildren;++i)
			{
				ev.target.getChildAt(i).backgroundColor = 0x777777;
				ev.target.getChildAt(i).textColor = 0;
					
			}
			if (!engineToBuy)
				shopScreen.changeStatusEngine(null);
			selectedTextFieldContainer = null;
			selectedEngine = null;
			
		}
		private function onMouseUp(ev:MouseEvent):void
		{
			mouseDown = false;
		}
		private function onMouseClick(ev:MouseEvent):void
		{
			
			
		}
		private function onMouseDown(ev:MouseEvent):void
		{
			trace (ev.localY);
			mouseDown = true;
			previousMouseY = ev.localY;
			
		}
		private function onMouseMove(ev:MouseEvent):void
		{
			/*
			if (mouseDown)
			{
				var scrollY:int = previousMouseY- ev.localY;
				trace (scrollY);
				var rect:Rectangle = canvas.scrollRect;
				rect.y+=scrollY;
				canvas.scrollRect = rect;
			}
			previousMouseY = ev.localY;
			*/
		}
		
	}
}